package com.fulei.logic;

import android.annotation.SuppressLint;

import com.bjfl.saleManager.util.LogUtil;
import com.dwin.navy.serialportapi.SerailPortOpt;

@SuppressLint({"SdCardPath"})
public class CoorDinateGood
{
  private static SerailPortOpt serialPort = null;
  public static String comNo = "";
  public static String baudRate = "";
  
  public static boolean OpenPort()
  {
    try
    {
      serialPort = new SerailPortOpt();
      
      serialPort.mDevNum = Integer.parseInt(comNo);
      serialPort.mSpeed = Integer.parseInt(baudRate);
      serialPort.mDataBits = 8;
      serialPort.mStopBits = 1;
      serialPort.mParity = 110;
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
    return true;
  }
  
  public static void closeSerialPort()
  {
    try
    {
      if (serialPort.mFd != null) {
        serialPort.closeDev(serialPort.mFd);
      }
      serialPort.mFd = null;
      serialPort = null;
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog(ex.getMessage());
    }
  }
  
  public static int ResetGoods()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 126, 10, 0, 1, 34, 0, 0, 1, -84, -19 };
      
      LogUtil.writeShipeMentInfo("ResetGoodSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 10);
      if (ReadDataLength < 10) {
        return -1;
      }
      LogUtil.writeShipeMentInfo("ResetGoodsRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -94)) {
          if ((ReadData[7] & 0x1) == 1) {
            return_data = 1;
          } else {
            return_data = -1;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeShipeMentInfo("复位货道异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int ResetGoods(int addr)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 126;
      SendData[1] = 10;
      SendData[2] = 0;
      SendData[3] = ((byte)addr);
      SendData[4] = 34;
      SendData[5] = 0;
      SendData[6] = 0;
      SendData[7] = 1;
      SendData[8] = SumByte(SendData, 0, 8);
      SendData[9] = -19;
      
      LogUtil.writeShipeMentInfo("ResetGoodSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 10);
      if (ReadDataLength < 10) {
        return -1;
      }
      LogUtil.writeShipeMentInfo("ResetGoodsRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -94)) {
          if ((ReadData[7] & 0x1) == 1) {
            return_data = 1;
          } else {
            return_data = -1;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeShipeMentInfo("复位货道异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int ShipMentStatus()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 126, 10, 0, 1, 17, 0, 0, 1, -101, -19 };
      
      LogUtil.writeShipeMentInfo("ShipMentStatusSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 17);
      if (ReadDataLength < 17) {
        return -1;
      }
      Thread.sleep(500L);
      LogUtil.writeShipeMentInfo("ShipMentStatusRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -111)) {
          if (ReadData[6] == 0)
          {
            if ((ReadData[8] >> 5 & 0x1) != 0)
            {
              LogUtil.writeShipeMentInfo("出货成功,关门成功(出货成功可以下次购买)+14");
              return_data = 14;
            }
            if ((ReadData[8] >> 5 & 0x1) == 0)
            {
              LogUtil.writeShipeMentInfo("出货失败+25");
              return_data = 25;
            }
          }
          else if (ReadData[6] == 1)
          {
            if ((ReadData[8] >> 5 & 0x1) != 0)
            {
              LogUtil.writeShipeMentInfo("货物被取走(货物已掉出但是没有归位)+27");
              return_data = 27;
            }
            else
            {
              LogUtil.writeShipeMentInfo("货到忙碌不能出货+21");
              return_data = 21;
            }
            if ((ReadData[8] >> 7 & 0x1) != 0)
            {
              LogUtil.writeShipeMentInfo("关门失败+29");
              return_data = 29;
            }
          }
          else if (ReadData[6] == 2)
          {
            return_data = 22;
          }
          else if (ReadData[6] == 3)
          {
            LogUtil.writeShipeMentInfo("传感器故障+26");
            return_data = 26;
          }
          else if ((ReadData[11] != 0) && (ReadData[12] != 0) && (ReadData[13] != 0))
          {
            LogUtil.writeShipeMentInfo("货道异常,请分析错误日志!!");
          }
          else if (ReadData[14] != 0)
          {
            if ((ReadData[14] & 0x1) == 1) {
              LogUtil.writeShipeMentInfo("ShipMentStatusRead:1前置电机故障");
            }
            if ((ReadData[14] & 0x2) == 2) {
              LogUtil.writeShipeMentInfo("ShipMentStatusRead:2后置电机故障");
            }
            if ((ReadData[14] & 0x3) == 3) {
              LogUtil.writeShipeMentInfo("ShipMentStatusRead:3左右电机故障");
            }
            if ((ReadData[14] & 0x4) == 4) {
              LogUtil.writeShipeMentInfo("ShipMentStatusRead:4推货电机故障");
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("获取货道状态:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int ShipMentStatus(int addr)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 126;
      SendData[1] = 10;
      SendData[2] = 0;
      SendData[3] = ((byte)addr);
      SendData[4] = 17;
      SendData[5] = 0;
      SendData[6] = 0;
      SendData[7] = 1;
      SendData[8] = SumByte(SendData, 0, 8);
      SendData[9] = -19;
      
      LogUtil.writeShipeMentInfo("ShipMentStatusSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 17);
      if (ReadDataLength < 17) {
        return -1;
      }
      LogUtil.writeShipeMentInfo("ShipMentStatusRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -111) && 
          (ReadData[4] == -111)) {
          if (ReadData[6] == 0)
          {
            if ((ReadData[8] >> 5 & 0x1) != 0)
            {
              LogUtil.writeShipeMentInfo("出货成功,关门成功(出货成功可以下次购买)+14");
              return_data = 14;
            }
            if ((ReadData[8] >> 5 & 0x1) == 0)
            {
              LogUtil.writeShipeMentInfo("出货失败+25");
              return_data = 25;
            }
          }
          else if (ReadData[6] == 1)
          {
            if ((ReadData[8] >> 5 & 0x1) != 0)
            {
              LogUtil.writeShipeMentInfo("货物被取走(货物已掉出但是没有归位)+27");
              return_data = 27;
            }
            else
            {
              LogUtil.writeShipeMentInfo("货到忙碌不能出货+21");
              return_data = 21;
            }
            if ((ReadData[8] >> 7 & 0x1) != 0)
            {
              LogUtil.writeShipeMentInfo("关门失败+29");
              return_data = 29;
            }
          }
          else if (ReadData[6] == 2)
          {
            return_data = 22;
          }
          else if (ReadData[6] == 3)
          {
            LogUtil.writeShipeMentInfo("传感器故障+26");
            return_data = 26;
          }
          else if ((ReadData[11] != 0) && (ReadData[12] != 0) && (ReadData[13] != 0))
          {
            LogUtil.writeShipeMentInfo("货道异常,请分析错误日志!!");
          }
          else if (ReadData[14] != 0)
          {
            if ((ReadData[14] & 0x1) == 1) {
              LogUtil.writeShipeMentInfo("ShipMentStatusRead:1前置电机故障");
            }
            if ((ReadData[14] & 0x2) == 2) {
              LogUtil.writeShipeMentInfo("ShipMentStatusRead:2后置电机故障");
            }
            if ((ReadData[14] & 0x3) == 3) {
              LogUtil.writeShipeMentInfo("ShipMentStatusRead:3左右电机故障");
            }
            if ((ReadData[14] & 0x4) == 4) {
              LogUtil.writeShipeMentInfo("ShipMentStatusRead:4推货电机故障");
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("获取货道状态:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int CoorDinateCaseGood(int YLine, int XLine)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[14];
      SendData[0] = 126;
      SendData[1] = 14;
      SendData[2] = 0;
      SendData[3] = 1;
      SendData[4] = 53;
      SendData[5] = 0;
      SendData[6] = 2;
      

      SendData[7] = ((byte)((YLine & 0xFF00) >> 8));
      SendData[8] = ((byte)(YLine & 0xFF));
      

      SendData[9] = 3;
      SendData[10] = ((byte)((XLine & 0xFF00) >> 8));
      SendData[11] = ((byte)(XLine & 0xFF));
      
      SendData[12] = SumByte(SendData, 0, 12);
      SendData[13] = -19;
      
      LogUtil.writeShipeMentInfo("CoorDinateCaseGoodSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 16);
      if (ReadDataLength < 16) {
        return -1;
      }
      LogUtil.writeShipeMentInfo("CoorDinateCaseGoodRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -75)) {
          if ((ReadData[13] & 0x1) == 1) {
            return_data = 1;
          } else {
            return_data = -1;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("电机测试后置和左右电机异常异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int CoorDinateCaseGood(int addr, int YLine, int XLine)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[14];
      SendData[0] = 126;
      SendData[1] = 14;
      SendData[2] = 0;
      SendData[3] = ((byte)addr);
      SendData[4] = 53;
      SendData[5] = 0;
      SendData[6] = 2;
      

      SendData[7] = ((byte)((YLine & 0xFF00) >> 8));
      SendData[8] = ((byte)(YLine & 0xFF));
      

      SendData[9] = 3;
      SendData[10] = ((byte)((XLine & 0xFF00) >> 8));
      SendData[11] = ((byte)(XLine & 0xFF));
      
      SendData[12] = SumByte(SendData, 0, 12);
      SendData[13] = -19;
      
      LogUtil.writeShipeMentInfo("CoorDinateCaseGoodSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 16);
      if (ReadDataLength < 16) {
        return -1;
      }
      LogUtil.writeShipeMentInfo("CoorDinateCaseGoodRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -75)) {
          if ((ReadData[13] & 0x1) == 1) {
            return_data = 1;
          } else {
            return_data = -1;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("电机测试后置和左右电机异常异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int PreposeMotorCase(int ZLine)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[11];
      SendData[0] = 126;
      SendData[1] = 11;
      SendData[2] = 0;
      SendData[3] = 1;
      SendData[4] = 54;
      SendData[5] = 0;
      SendData[6] = 1;
      

      SendData[7] = ((byte)((ZLine & 0xFF00) >> 8));
      SendData[8] = ((byte)(ZLine & 0xFF));
      
      SendData[9] = SumByte(SendData, 0, 9);
      SendData[10] = -19;
      
      LogUtil.writeShipeMentInfo("PreposeMotorCaseSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 13);
      if (ReadDataLength < 13) {
        return -1;
      }
      LogUtil.writeShipeMentInfo("PreposeMotorCaseRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -74)) {
          if ((ReadData[10] & 0x1) == 1)
          {
            LogUtil.writeShipeMentInfo("设置成功标志");
            return_data = 1;
          }
          else
          {
            return_data = -1;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("电机测试前置电机异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int PreposeMotorCase(int addr, int ZLine)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[11];
      SendData[0] = 126;
      SendData[1] = 11;
      SendData[2] = 0;
      SendData[3] = ((byte)addr);
      SendData[4] = 54;
      SendData[5] = 0;
      SendData[6] = 1;
      

      SendData[7] = ((byte)((ZLine & 0xFF00) >> 8));
      SendData[8] = ((byte)(ZLine & 0xFF));
      
      SendData[9] = SumByte(SendData, 0, 9);
      SendData[10] = -19;
      
      LogUtil.writeShipeMentInfo("PreposeMotorCaseSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 13);
      if (ReadDataLength < 13) {
        return -1;
      }
      Thread.sleep(1000L);
      LogUtil.writeShipeMentInfo("PreposeMotorCaseRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -74)) {
          if ((ReadData[10] & 0x1) == 1)
          {
            LogUtil.writeShipeMentInfo("设置成功标志");
            return_data = 1;
          }
          else
          {
            return_data = -1;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("电机测试前置电机异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int goodsSellCase(int ZLine, int YLine, int XLine)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[17];
      SendData[0] = 126;
      SendData[1] = 17;
      SendData[2] = 0;
      SendData[3] = 1;
      SendData[4] = 55;
      SendData[5] = 0;
      
      SendData[6] = 1;
      

      SendData[7] = ((byte)((ZLine & 0xFF00) >> 8));
      SendData[8] = ((byte)(ZLine & 0xFF));
      
      SendData[9] = 2;
      

      SendData[10] = ((byte)((YLine & 0xFF00) >> 8));
      SendData[11] = ((byte)(YLine & 0xFF));
      

      SendData[12] = 3;
      

      SendData[13] = ((byte)((XLine & 0xFF00) >> 8));
      SendData[14] = ((byte)(XLine & 0xFF));
      
      SendData[15] = SumByte(SendData, 0, 15);
      SendData[16] = -19;
      
      LogUtil.writeShipeMentInfo("goodsSellCaseSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 19);
      if (ReadDataLength < 19) {
        return -1;
      }
      Thread.sleep(1000L);
      
      LogUtil.writeShipeMentInfo("goodsSellCaseRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -73))
        {
          if ((ReadData[16] & 0x1) == 1) {
            return 1;
          }
          return -1;
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("出货指令异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int goodsSellCase(int addr, int ZLine, int YLine, int XLine)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[17];
      SendData[0] = 126;
      SendData[1] = 17;
      SendData[2] = 0;
      SendData[3] = ((byte)addr);
      SendData[4] = 55;
      SendData[5] = 0;
      
      SendData[6] = 1;
      

      SendData[7] = ((byte)((ZLine & 0xFF00) >> 8));
      SendData[8] = ((byte)(ZLine & 0xFF));
      
      SendData[9] = 2;
      

      SendData[10] = ((byte)((YLine & 0xFF00) >> 8));
      SendData[11] = ((byte)(YLine & 0xFF));
      

      SendData[12] = 3;
      

      SendData[13] = ((byte)((XLine & 0xFF00) >> 8));
      SendData[14] = ((byte)(XLine & 0xFF));
      
      SendData[15] = SumByte(SendData, 0, 15);
      SendData[16] = -19;
      
      LogUtil.writeShipeMentInfo("goodsSellCaseSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 19);
      if (ReadDataLength < 19) {
        return -1;
      }
      LogUtil.writeShipeMentInfo("goodsSellCaseRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -73)) {
          if ((ReadData[16] & 0x1) == 1) {
            return_data = 1;
          } else {
            return_data = -1;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("出货指令异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int CoorDinateHome(int YLine, int XLine)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 126;
      SendData[1] = 10;
      SendData[2] = 0;
      SendData[3] = 1;
      SendData[4] = 56;
      SendData[5] = 0;
      
      SendData[6] = ((byte)YLine);
      SendData[7] = ((byte)XLine);
      
      SendData[8] = SumByte(SendData, 0, 8);
      SendData[9] = -19;
      LogUtil.writeShipeMentInfo("CoorDinateHomeSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 12);
      if (ReadDataLength < 12) {
        return -1;
      }
      LogUtil.writeShipeMentInfo("CoorDinateHomeRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if (ReadData[(length - 1)] == -19) {
          if (ReadData[4] == -72) {
            if ((ReadData[9] & 0x1) == 1) {
              return_data = 1;
            } else {
              return_data = -1;
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("后置和左右电机归位异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int CoorDinateHome(int addr, int YLine, int XLine)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 126;
      SendData[1] = 10;
      SendData[2] = 0;
      SendData[3] = ((byte)addr);
      SendData[4] = 56;
      SendData[5] = 0;
      
      SendData[6] = ((byte)YLine);
      SendData[7] = ((byte)XLine);
      
      SendData[8] = SumByte(SendData, 0, 8);
      SendData[9] = -19;
      LogUtil.writeShipeMentInfo("CoorDinateHomeSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 12);
      if (ReadDataLength < 12) {
        return -1;
      }
      LogUtil.writeShipeMentInfo("CoorDinateHomeRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if (ReadData[(length - 1)] == -19) {
          if (ReadData[4] == -72) {
            if ((ReadData[9] & 0x1) == 1) {
              return_data = 1;
            } else {
              return_data = -1;
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("后置和左右电机归位异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int PreposeMotorHome(int ZLine)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 126;
      SendData[1] = 9;
      SendData[2] = 0;
      SendData[3] = 1;
      SendData[4] = 57;
      SendData[5] = 0;
      
      SendData[6] = ((byte)ZLine);
      
      SendData[7] = SumByte(SendData, 0, 7);
      SendData[8] = -19;
      
      LogUtil.writeShipeMentInfo("PreposeMotorHomeSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 11);
      if (ReadDataLength < 11) {
        return -1;
      }
      LogUtil.writeShipeMentInfo("PreposeMotorHomeRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if (ReadData[(length - 1)] == -19) {
          if (ReadData[4] == -71) {
            if ((ReadData[8] & 0x1) == 1) {
              return_data = 1;
            } else {
              return_data = -1;
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("前置电机归位异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int PreposeMotorHome(int addr, int ZLine)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 126;
      SendData[1] = 9;
      SendData[2] = 0;
      SendData[3] = ((byte)addr);
      SendData[4] = 57;
      SendData[5] = 0;
      
      SendData[6] = ((byte)ZLine);
      
      SendData[7] = SumByte(SendData, 0, 7);
      SendData[8] = -19;
      
      LogUtil.writeShipeMentInfo("PreposeMotorHomeSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 11);
      if (ReadDataLength < 11) {
        return -1;
      }
      LogUtil.writeShipeMentInfo("PreposeMotorHomeRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if (ReadData[(length - 1)] == -19) {
          if (ReadData[4] == -71) {
            if ((ReadData[8] & 0x1) == 1) {
              return_data = 1;
            } else {
              return_data = -1;
            }
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("前置电机归位异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int pocketSellCase()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 110;
      SendData[1] = 10;
      SendData[2] = 0;
      SendData[3] = 1;
      SendData[4] = 33;
      SendData[5] = 0;
      SendData[6] = 0;
      SendData[7] = 0;
      SendData[8] = SumByte(SendData, 0, 8);
      SendData[9] = -19;
      
      LogUtil.writePocketInfo("pocketSellCaseSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 13);
      if (ReadDataLength < 13) {
        return -1;
      }
      LogUtil.writePocketInfo("pocketSellCaseRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 126)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -95)) {
          if (ReadData[7] == 1) {
            return_data = 1;
          } else {
            return_data = -1;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("出袋指令异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int pocketStatus()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 110;
      SendData[1] = 10;
      SendData[2] = 0;
      SendData[3] = 1;
      SendData[4] = 17;
      SendData[5] = 0;
      SendData[6] = 0;
      SendData[7] = 1;
      SendData[8] = SumByte(SendData, 0, 8);
      SendData[9] = -19;
      
      LogUtil.writePocketInfo("pocketStatusSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 17);
      if (ReadDataLength < 17) {
        return -1;
      }
      LogUtil.writePocketInfo("pocketStatusRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 110)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -111)) {
          if (ReadData[6] == 0)
          {
            if ((ReadData[8] >> 5 & 0x1) != 0)
            {
              LogUtil.writePocketInfo("出袋成功+14");
              return_data = 14;
            }
            if ((ReadData[8] >> 5 & 0x1) == 0)
            {
              LogUtil.writePocketInfo("出袋失败+25");
              return_data = 25;
            }
          }
          else if (ReadData[6] == 1)
          {
            if ((ReadData[8] >> 5 & 0x1) != 0)
            {
              LogUtil.writePocketInfo("袋被取走+27");
              return_data = 27;
            }
            else
            {
              LogUtil.writePocketInfo("设备处于忙碌状态+21");
              return_data = 21;
            }
          }
          else if (ReadData[6] == 2)
          {
            return_data = 22;
          }
          else if (ReadData[6] == 3)
          {
            LogUtil.writePocketInfo("传感器故障+26");
            return_data = 26;
          }
          else if (ReadData[6] == 4)
          {
            LogUtil.writePocketInfo("食品袋将尽，可以继续出袋+31");
            return_data = 31;
          }
          else if ((ReadData[11] != 0) && (ReadData[12] != 0) && (ReadData[13] != 0))
          {
            LogUtil.writePocketInfo("出袋设备异常,请分析错误日志!!");
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("获取食品袋状态:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int pocketMotorForward(int times)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 110;
      SendData[1] = 10;
      SendData[2] = 0;
      SendData[3] = 1;
      SendData[4] = 18;
      SendData[5] = 0;
      SendData[6] = ((byte)times);
      SendData[7] = 0;
      SendData[8] = SumByte(SendData, 0, 8);
      SendData[9] = -19;
      
      LogUtil.writePocketInfo("pocketMotorForwardSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 10);
      if (ReadDataLength < 10) {
        return -1;
      }
      LogUtil.writePocketInfo("pocketMotorForwardRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 110)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -110)) {
          if (ReadData[7] == 1) {
            return_data = 1;
          } else {
            return_data = -1;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("电机正转指令异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int pocketMotorReversal(int times)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 110;
      SendData[1] = 10;
      SendData[2] = 0;
      SendData[3] = 1;
      SendData[4] = 19;
      SendData[5] = 0;
      SendData[6] = ((byte)times);
      SendData[7] = 0;
      SendData[8] = SumByte(SendData, 0, 8);
      SendData[9] = -19;
      
      LogUtil.writePocketInfo("pocketMotorForwardSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 10);
      if (ReadDataLength < 10) {
        return -1;
      }
      LogUtil.writePocketInfo("pocketMotorForwardRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 110)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -109)) {
          if (ReadData[7] == 1) {
            return_data = 1;
          } else {
            return_data = -1;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("电机反转指令异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int pocketMotorSpeed(int speed)
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = new byte[10];
      SendData[0] = 110;
      SendData[1] = 10;
      SendData[2] = 0;
      SendData[3] = 1;
      SendData[4] = 20;
      SendData[5] = 0;
      SendData[6] = ((byte)speed);
      SendData[7] = 0;
      SendData[8] = SumByte(SendData, 0, 8);
      SendData[9] = -19;
      
      LogUtil.writePocketInfo("pocketMotorSpeedSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 10);
      if (ReadDataLength < 10) {
        return -1;
      }
      LogUtil.writePocketInfo("pocketMotorSpeedRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 110)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -108)) {
          if (ReadData[7] == 1) {
            return_data = 1;
          } else {
            return_data = -1;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("调节电机速度指令异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static int pocketReset()
  {
    int return_data = 0;
    try
    {
      if (serialPort == null) {
        OpenPort();
      }
      if (serialPort.mFd == null)
      {
        serialPort.openDev(serialPort.mDevNum);
        serialPort.setSpeed(serialPort.mFd, serialPort.mSpeed);
        serialPort.setParity(serialPort.mFd, serialPort.mDataBits, serialPort.mStopBits, serialPort.mParity);
      }
      byte[] SendData = { 110, 10, 0, 1, 34, 0, 0, 1, -100, -19 };
      
      LogUtil.writePocketInfo("pocketResetSend:" + LogUtil.bytesToHexString(SendData, SendData.length));
      if (null != serialPort.mFd) {
        serialPort.writeBytes(SendData);
      }
      byte[] ReadData = new byte[512];
      int ReadDataLength = serialPort.readBytes(ReadData, 200, 10);
      if (ReadDataLength < 10) {
        return -1;
      }
      LogUtil.writePocketInfo("pocketResetRead:" + LogUtil.bytesToHexString(ReadData, ReadDataLength));
      if ((ReadData[0] & 0xFF) == 110)
      {
        byte length = 0;
        length = (byte)(ReadData[1] + ReadData[2] * 256);
        if ((ReadData[(length - 1)] == -19) && 
          (ReadData[4] == -94)) {
          if ((ReadData[7] & 0x1) == 1) {
            return_data = 1;
          } else {
            return_data = -1;
          }
        }
      }
    }
    catch (Exception ex)
    {
      LogUtil.writeExceptionLog("食品袋复位异常:" + ex.getMessage());
      return_data = -1;
    }
    return return_data;
  }
  
  public static byte SumByte(byte[] data, int offset, int length)
  {
    int Result = 0;
    for (int i = offset; i <= offset + length; i++) {
      Result += data[i];
    }
    return (byte)Result;
  }
}
